﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ChineseChess.Game.AI
{
    public class Ai3: BaseAi
    {

        /// <summary>
        /// Ai的算法
        /// </summary>
        /// <param name="pieceList">当前局面</param>
        /// <param name="canvas"></param>
        /// <param name="deep">计算深度</param>
        public static void Run(List<Piece> pieceList, Canvas canvas, int deep = 3)
        {
            List<Piece> pies = pieceList;
            Moves mv = search(pies, deep, false, 0);

            Piece p = clearPiece(pieceList, (int)mv.p.X, (int)mv.p.Y);//吃掉子
            //MessageBox.Show(mv.pie.Name.ToString() + mv.pie.X + "," + mv.pie.Y + ", " + mv.p.ToString());
            if (p != null)
            {
                canvas.Children.Remove(p);
            }

            for (int i = 0; i < pieceList.Count; i++)
            {
                var pie = pieceList[i];
                if (pie.X == mv.pie.X && pie.Y == mv.pie.Y)
                {
                    pie.X = (int)mv.p.X;
                    pie.Y = (int)mv.p.Y;
                    pie.InvalidateVisual();
                }
            }
        }

        static int MIN_SCORE = -9999;
        static int MAX_SCORE = 9999;
        public static Moves search(List<Piece> pieceList, int deep,bool isred,int now_best)
        {
            var mvs = generateMoves(pieceList, isred);

            int score;
            Moves result;
            int best_id = 0;
            int best_score = MIN_SCORE;
            if(isred)
            {
                best_score = MAX_SCORE;
            }

            for (int i = 0; i < mvs.Count; i++)
            {
                var mv = mvs[i];
                Moves historyMv;
                Piece pie = MovePie(pieceList, mv, out historyMv);
                if(deep ==1)
                {
                    score = getScore(pieceList, isred);
                }
                else
                {
                    score = getScore(pieceList, isred);
                    if (Math.Abs(score) < 9000)
                    {
                        result = search(pieceList, deep - 1, !isred, best_score);
                        score = result.score;
                    }
                }
                BackMovePie(pieceList, pie, historyMv);


                // 更新最优走法
                if ((!isred && score>=best_score) || (isred && score <= best_score))
                {
                    if(!(Math.Abs(score - best_score) == 0))
                    {
                        best_id = i;
                        best_score = score;
                    }
                }

                // a - b 剪子
                if(isred && now_best >best_score)
                {
                    result = mvs[best_id];
                    result.score = best_score;
                    return result;
                }
                else if(!isred && now_best<best_score)
                {
                    result = mvs[best_id];
                    result.score = best_score;
                    return result;
                } 
            }

            result =  mvs[best_id];
            result.score = best_score;
            return result;
        } 

    }
}
